'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
var wx_mini_appid = exports.wx_mini_appid = 'wx4015a847131516ad';
// var ali_mini_appid = exports.ali_mini_appid = '2021001166607187';
var ali_mini_appid = exports.ali_mini_appid = '2021002107655646';
var wx_app_appid = exports.wx_app_appid = 'wx1ab6ac4dcedfb167';
var ali_app_appid = exports.ali_app_appid = '2021001179600149';
var xj_itemCode = exports.xj_itemCode = 'IC_CASH_ST';
var rs_itemCode = exports.rs_itemCode = 'IC-warter';
var XK_MODE = exports.XK_MODE = 'XK_MODE';
var JF_MODE = exports.JF_MODE = 'JF_MODE';
let portalCode = '';
// #ifdef MP-WEIXIN
portalCode = 'POC_WX_APPLET';
// #endif
// #ifdef MP-ALIPAY
portalCode = 'POC_ZFB_APPLET';
// #endif
// #ifdef H5
portalCode = 'POC_WX_OPENPLAFORM';
// #endif
// #ifdef APP-PLUS
switch (uni.getSystemInfoSync().platform) {
  case 'android':
	portalCode = 'POC_APP_ANDRIOD'
	break;
  case 'ios':
	portalCode = 'POC_APP_APPLE'
	break;
  default:
	console.log('运行在开发者工具上')
	break;
}
// #endif

var failFnModal = function failFnModal(content) {
	return function () {
		for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
			args[_key] = arguments[_key];
		}
		args[0].showMcModal = true
		args[0].title = '温馨提示'
		args[0].content = content
		args[0].okBtn = '确定'
		args[0].cc = () => {
			args[0].showMcModal = false
		}
	};
};

var showLoading = function showLoading(title) {
	uni.showLoading({
		title: title
	});
};

import { getMilkPayChannel, getMilkOpenId, payMilk, paymentMilk, getMilkAlipayAccessToken } from '../api/index.js';

var commonPay = exports.commonPay = function commonPay(mode, paramsObj) {
	return function () {
		for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
			args[_key] = arguments[_key];
		}
		_payChannel.apply(undefined, [mode, paramsObj].concat(args));
	};
};

export const appPayList = (mode, paramsObj) => (...args) => {
	return _payChannel(mode, paramsObj, ...args)
}

const _payChannel = (mode, paramsObj, ...args) => {
	return new Promise((resolve, reject) => {
		getMilkPayChannel({
			orgId: paramsObj.orgId,
			portalCode
		}).then(res => {
			if (res.code == 0) {
				if (res.data.length == 0) {
					uni.hideLoading()
					failFnModal('没有对应的支付渠道，请配置支付渠道！')(args[1])
				} else {
					uni.hideLoading()
					// #ifdef APP-PLUS
					resolve(res.data);
					// #endif
					// #ifdef MP-WEIXIN
					_loginAuthorize(mode, paramsObj, res.data, ...args)
					// #endif
					// #ifdef MP-ALIPAY
					_loginAuthorize(mode, paramsObj, res.data, ...args)
					// #endif
					// #ifdef H5
					mode === XK_MODE ? _pay(mode, paramsObj, res.data, ...args) : _payment(mode, paramsObj, res.data, ...args)
					// #endif
				}
			} else {
				uni.hideLoading()
				failFnModal()(res.msg)
			}
		}).catch(err => {
			uni.hideLoading()
			console.log(err)
		})
	})
}

export function _loginAuthorize(mode, paramsObj, data, ...params) {
	// #ifdef MP-WEIXIN
	showLoading('授权中...')
	uni.login({
		provider: 'weixin',
		success: function (response) {
			if (response.errMsg == "login:ok") {
				_getUserOpenId(mode, response.code, paramsObj, data, ...params)
			} else {
				uni.hideLoading()
				failFnModal('微信授权异常')()
			}
		},
		fail: function(error) {
			uni.hideLoading()
			console.log(error)
		}
	})
	// #endif
	// #ifdef MP-ALIPAY
	showLoading('授权中...')
	uni.login({
		provider: 'alipay',
		success: function (response) {
			if (response.errMsg == "login:ok") {
				_getUserOpenId(mode, response.code, paramsObj, data, ...params)
			}
		},
		fail:function(err){
			uni.hideLoading()
			failFnModal('支付宝授权异常')()
		}
	})
	// #endif
}

export function _getUserOpenId(...params) {
	const [mode, code, ...obj] = params;
	// #ifdef MP-WEIXIN
	getMilkOpenId({
		xappId: wx_mini_appid,
		code
	}).then(res => {
		if(res.resultCode == 0) {
			mode === XK_MODE ? _pay(res.openid, ...obj) : _payment(res.openid, ...obj)
		} else {
			failFnModal()(res.errmsg)
		}
	}).catch(err => {
		console.log(err)
	})
	// #endif
	// #ifdef MP-ALIPAY
	getMilkAlipayAccessToken({
		xappId: ali_mini_appid,
		code
	}).then(res => {
		if (res.resultCode == 0) {
			mode === XK_MODE ? _pay(res.aliUserId, ...obj) : _payment(res.aliUserId, ...obj)
		} else {
			failFnModal()(res.resultMsg)
		}
	}).catch(err => {
		console.log(err)
	})
	// #endif
}

export function _payment(...params) {
	// #ifdef MP-WEIXIN
	showLoading('支付中...')
	const [openId, obj, data, stuObj, _that] = params;
	paymentMilk({
		amount: obj.amount,
		empId: stuObj.empId,
		payerAccount: openId,
		itemId: obj.itemId,
		xappId: wx_mini_appid,
		channelId: data.filter(item => item.channelName == "微信")[0].channelId,
		portalCode,
		orgId: obj.orgId
	}).then(res => {
		if(res.code == 0) {
			if(res.data.resultCode == 0) {
				uni.hideLoading()
				requestPayment(res.data, _that)
			} else {
				uni.hideLoading()
				failFnModal(res.data.resultMsg)(_that)
			}
		} else {
			uni.hideLoading()
			failFnModal(res.msg)(_that)
		}
	}).catch(err => {
		uni.hideLoading()
		console.log(err)
	})
	// #endif
	// #ifdef MP-ALIPAY
	showLoading('支付中...')
	const [payerAccount, obj, data, stuObj, _that] = params;
	paymentMilk({
		amount: obj.amount,
		empId: stuObj.empId,
		payerAccount,
		itemId: obj.itemId,
		xappId: ali_mini_appid,
		channelId: data.filter(item => item.channelName == "支付宝")[0].channelId,
		portalCode,
		orgId: obj.orgId
	}).then(res => {
		if(res.code == 0) {
			if(res.data.resultCode == 0) {
				uni.hideLoading()
				requestPayment(res.data, _that)
			} else {
				uni.hideLoading()
				failFnModal(res.data.resultMsg)(_that)
			}
		} else {
			uni.hideLoading()
			failFnModal(res.msg)(_that)
		}
	}).catch(err => {
		uni.hideLoading()
		console.log(err)
	})
	// #endif
	// #ifdef H5
	showLoading('支付中...')
	let xappId = uni.getStorageSync('str1');
	let xuserId = uni.getStorageSync('str2');
	const [mode, obj, data, stuObj, _that] = params;
	paymentMilk({
		amount: obj.amount,
		empId: stuObj.empId,
		payerAccount: xuserId,
		itemId: obj.itemId,
		xappId,
		channelId: data.filter(item => item.channelName == "微信")[0].channelId,
		portalCode,
		orgId: obj.orgId
	}).then(res => {
		if(res.code == 0) {
			if(res.data.resultCode == 0) {
				requestPayment(res.data, _that)
				uni.hideLoading()
			} else {
				uni.hideLoading()
				failFnModal(res.data.resultMsg)(_that)
			}
		} else {
			uni.hideLoading()
			failFnModal(res.msg)(_that)
		}
	}).catch(err => {
		console.log(err)
	})
	// #endif
	// #ifdef APP-PLUS
	const [obj, stuObj, channelId, _that] = params;
	showLoading('支付中...')
	paymentMilk({
		amount: obj.amount,
		empId: stuObj.empId,
		isApp: true,
		itemId: obj.itemId,
		xappId: channelId == 200 ? wx_app_appid : ali_app_appid,
		channelId,
		portalCode,
		orgId: obj.orgId
	}).then(res => {
		if(res.code == 0) {
			if(res.data.resultCode == 0) {
				requestPayment(res.data, _that)
				uni.hideLoading()
			} else {
				uni.hideLoading()
				errorTips(_that, res.data.resultMsg)
			}
		} else {
			uni.hideLoading()
			errorTips(_that, res.msg)
		}
	}).catch(err => {
		console.log(err)
	})
	// #endif
}

// 校卡下单
export function _pay(...params) {
	// #ifdef MP-WEIXIN
	let s_userId = uni.getStorageSync('s_userId')
	showLoading('支付中...')
	const [payerAccount, paramsObj, data, amount, _that] = params;
	payMilk({
		amount,
		xappId: wx_mini_appid,
		payerAccount,
		itemCode: paramsObj.walletType == 1 ? xj_itemCode : rs_itemCode,
		portalCode: portalCode,
		empId: paramsObj.empId,
		userId: s_userId ? s_userId : '',
		channelId: data.filter(item => item.channelName == "微信")[0].channelId,
		orgId: paramsObj.orgId,
		cardNo: paramsObj.cardNo
	}).then(res => {
		if (res.code == 0) {
			if (res.data.resultCode == 0) {
				uni.hideLoading()
				requestPayment(res.data, _that)
			} else {
				uni.hideLoading()
				failFnModal(res.data.resultMsg)(_that)
			}
		} else {
			uni.hideLoading()
			failFnModal(res.msg)(_that)
		}
	}).catch(err => {
		uni.hideLoading()
		console.log(err)
	})
	// #endif
	// #ifdef MP-ALIPAY
	showLoading('支付中...')
	const [payerAccount, paramsObj, data, amount, _that] = params;
	payMilk({
	  amount,
	  empId: paramsObj.empId,
	  payerAccount,
	  itemCode: paramsObj.walletType == 1 ? xj_itemCode : rs_itemCode,
	  xappId: ali_mini_appid,
	  channelId: data.filter(item => item.channelName == "支付宝")[0].channelId,
	  portalCode,
	  orgId: paramsObj.orgId,
	  cardNo: paramsObj.cardNo,
	}).then(res => {
		if (res.code == 0) {
			if (res.data.resultCode == 0) {
				uni.hideLoading()
				requestPayment(res.data, _that)
			} else {
				uni.hideLoading()
				failFnModal(res.data.resultMsg)(_that)
			}
		} else {
			uni.hideLoading()
			failFnModal(res.msg)(_that)
		}
	}).catch(err => {
		console.log(err)
	})
	// #endif
	// #ifdef H5
	showLoading('支付中...')
	let xappId = uni.getStorageSync('str1');
	let xuserId = uni.getStorageSync('str2');
	const [mode, paramsObj, data, amount, _that] = params;
	console.log(_that)
	payMilk({
		amount,
		empId: paramsObj.empId,
		payerAccount: xuserId,
		xappId,
		channelId: data.filter(item => item.channelName == "微信")[0].channelId,
		itemCode: paramsObj.walletType == 1 ? xj_itemCode : rs_itemCode,
		portalCode,
		orgId: paramsObj.orgId,
		cardNo: paramsObj.cardNo,
	}).then(res => {
		if (res.code == 0) {
			if (res.data.resultCode == 0) {
				requestPayment(res.data)
				uni.hideLoading()
			} else {
				uni.hideLoading()
				failFnModal(res.data.resultMsg)(_that)
			}
		} else {
			uni.hideLoading()
			failFnModal(res.msg)(_that)
		}
	})
	// #endif
	// #ifdef APP-PLUS
	showLoading('支付中...')
	const [amount, walletObj, channelId, _that] = params;
	payMilk({
		amount,
		empId: walletObj.empId,
		xappId: channelId == 200 ? wx_app_appid : ali_app_appid,
		channelId,
		itemCode: walletObj.walletType == 1 ? xj_itemCode : rs_itemCode,
		portalCode,
		orgId: walletObj.orgId,
		cardNo: walletObj.cardNo,
		isApp: true
	}).then(res => {
		if(res.code == 0) {
			if(res.data.resultCode == 0) {
				requestPayment(res.data, _that)
				uni.hideLoading()
			} else {
				uni.hideLoading()
				errorTips(_that, res.data.resultMsg)
			}
		} else {
			uni.hideLoading()
			errorTips(_that, res.msg)
		}
	}).catch(err => {
		uni.hideLoading()
		console.log(err)
	})
	// #endif
}

export function requestPayment(data, _that) {
	// #ifdef MP-WEIXIN
	uni.requestPayment({
		provider: 'wxpay',
		timeStamp: data.timeStamp,
		nonceStr: data.nonceStr,
		package: data.packages,
		signType: data.signType,
		paySign: data.paySign,
		success: function (resss) {
			if (resss.resultCode == 6001) {
				errorTips(_that, resss.memo)
			} else {
				successTips(_that)
			}
		}, fail: function (err) {
		  failFnModal('支付失败')(_that)
		}
	})
	// #endif
	// #ifdef MP-ALIPAY
	uni.requestPayment({
		provider: 'alipay',
		orderInfo: data.payResponseNo,
		success: function (resss) {
			if (resss.resultCode == 6001) {
				errorTips(_that, resss.memo)
			} else {
				successTips(_that)
			}
		},
		fail: function (err) {
		  failFnModal('支付失败')(_that)
		}
	})
	// #endif
	// #ifdef H5
	WeixinJSBridge.invoke('getBrandWCPayRequest', {
	  appId: data.xappId,
	  timeStamp: data.timeStamp,
	  nonceStr: data.nonceStr,
	  package: data.packages,
	  signType: data.signType,
	  paySign: data.paySign
	}, (res => {
	  if (res.err_msg) {
		uni.reLaunch({
		  url: '/pageMilk/index/index'
		})
	  } else {
		alert(JSON.stringify(res))
	  }
	}));
	// #endif
	// #ifdef APP-PLUS
	data.xappId === wx_app_appid ? requestAppWxPay(data, _that) : requestAppAliPay(data, _that)
	// #endif
}

function successTips(_that) {
	_that.showMcModal = true
	_that.title = '温馨提示'
	_that.content = '支付成功'
	_that.okBtn = '确定'
	_that.cc = () => {
		uni.reLaunch({
			url: '/pageMilk/index/index'
		})
	}
}

function errorTips(_that, content) {
	_that.showMcModal = true
	_that.title = '支付失败'
	_that.content = content
	_that.okBtn = '确定'
	_that.cc = () => {
		_that.showMcModal = false
	}
}

function requestAppAliPay(data, _that) {
	uni.requestPayment({
		provider: 'alipay',
		orderInfo: data.payParam,
		success: function (resss) {
			if (resss.resultCode == 6001) {
				failFnModal()(resss.memo)
			} else {
				successTips(_that)
			}
		},
		fail: function (err) {
			failFnModal('支付失败')(_that)
		}
	})
}

function requestAppWxPay(data, _that) {
	let obj = JSON.stringify({
		appid: data.xappId,
		noncestr: data.nonceStr,
		package: data.packages, // 固定值，以微信支付文档为主  
		partnerid: data.partnerid,
		prepayid: data.prepayid,
		timestamp: Number(data.timeStamp),
		sign: data.signType // 根据签名算法生成签名
	})
	uni.requestPayment({
		provider: 'wxpay',
		orderInfo: obj,
		success: function (res) {
			uni.reLaunch({
				url: '/pageMilk/index/index'
			})
		},
		fail: function (err) {
			console.log('fail:' + JSON.stringify(err));
			failFnModal('支付失败')(_that)
		}
	})
}